package com.biubiux2.message;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;

import java.nio.charset.StandardCharsets;

/**
 * @author Lijinbao
 * @since 8/8/2022 7:58 PM
 */
public class MessageUtil {

    public static ByteBuf createLoginSuccessMsg(String sessionId) {
        ByteBuf response = PooledByteBufAllocator.DEFAULT.directBuffer();
        response.writeInt(MessageType.LOGIN_RESPONSE);
        response.writeBytes(sessionId.getBytes(StandardCharsets.UTF_8));
        return response;
    }

    public static ByteBuf createLoginFailMsg() {
        ByteBuf response = PooledByteBufAllocator.DEFAULT.directBuffer();
        response.writeInt(MessageType.ERROR);
        return response;
    }

    public static ByteBuf createReconnectMsg(boolean success) {
        ByteBuf response = PooledByteBufAllocator.DEFAULT.directBuffer();
        if (success) {
            response.writeInt(MessageType.RECONNECT_SUCCESS_RESPONSE);
        } else {
            response.writeInt(MessageType.RECONNECT_FAIL_RESPONSE);
        }
        return response;
    }

    public static ByteBuf createReLoginMsg() {
        ByteBuf response = PooledByteBufAllocator.DEFAULT.directBuffer();
        response.writeInt(MessageType.RELOGIN);
        return response;
    }
}
